home *** CD-ROM | disk | FTP | other *** search
/ Internet Publisher's Toolbox 2.0 / Internet Publisher's Toolbox.iso / internet / ntserver / wtsource / irkeywor.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-08  |  3.8 KB  |  142 lines

  1. /* copyright (c) CNIDR (see ../COPYRIGHT)
  2.  
  3. 7/29/92
  4.  
  5. This program is an attempt to scan into the dictionary and inverted file to determine the keywords that best
  6. describe a database.  These could then be included in the description file
  7. $Log:   irkeywords.c,v $
  8.  * Revision 1.3  93/06/23  19:57:52  warnock
  9.  * Fix from tovio@sage.ucs.uwa.edu.au for empty keywords array
  10.  * 
  11.  * Revision 1.2  93/02/16  17:07:49  freewais
  12.  * added AT&T patches for keyword list
  13.  * 
  14.  
  15. */
  16.  
  17. #include <string.h>
  18. #include <sys/types.h>
  19. #ifndef WIN32
  20. #include <sys/param.h>
  21. #endif
  22. #include "irdirent.h"
  23. #include "cutil.h"
  24. #include "futil.h"
  25. #include "irfiles.h"
  26. #include "irtfiles.h"
  27. #include "panic.h"
  28. #include "ircfiles.h"
  29. #include "version.h"
  30. #include "irext.h"
  31. #include "irlex.h"
  32.  
  33. #ifdef WIN32
  34. boolean look_up_total_word_count(database*);
  35. #endif
  36.  
  37. FILE *logfile;
  38.  
  39. unsigned char *dictionary_header = NULL; /* the dictionary header. 
  40.                           loaded once */
  41.  
  42. long number_of_blocks = 0;  /* also the length of the dictionary 
  43.                       header block */
  44.  
  45. unsigned char *dictionary = NULL; /* this is one of the dict blocks */
  46.  
  47.   char *keyword[100];
  48.   long keyvalue[100];
  49.   long stored;
  50.   short nKeys = 0;
  51.  
  52. retreive_keywords(db)
  53.   database* db;
  54. {
  55.   long i,j, k, l, tmp, limit;
  56.   char file[MAX_FILE_NAME_LEN + 1 ];
  57. #ifdef WIN32
  58.   double x;
  59. #else
  60.   double x,y;
  61. #endif
  62.   char *ptr;
  63. int tmpval;
  64.  
  65.   if(NULL == dictionary_header) {
  66.     FILE *stream = db->dictionary_stream;
  67.     s_fclose(stream);
  68.     db->dictionary_stream = s_fopen(dictionary_filename(file, db), "r+b");
  69.     stream = db->dictionary_stream;
  70.     s_fseek(stream, 0L, SEEK_SET);
  71.     number_of_blocks = read_bytes(DICTIONARY_HEADER_SIZE,stream);
  72.     dictionary_header=
  73.       read_dictionary_block(dictionary_header,DICTIONARY_HEADER_SIZE,
  74.                 number_of_blocks,stream);
  75.     if(NULL == dictionary_header) {
  76.       printf("Could not read dictionary header block in db %s.", db->database_file);
  77.       return(0);
  78.     }
  79.   }
  80.   look_up_total_word_count(db);
  81.     stored = 0;
  82.   for (i=0; i<=number_of_blocks; i++) {
  83.     FILE *stream = db->dictionary_stream;
  84.     dictionary = read_dictionary_block(dictionary,dictionary_block_position(i, dictionary_header),
  85.                        DICTIONARY_BLOCK_SIZE, stream);
  86.     for (j=0; j<=DICTIONARY_BLOCK_SIZE; j++)
  87.       if (strlen(dictionary_block_word(j, dictionary))) {
  88.       tmp = dictionary_block_word_occurances(j, dictionary);
  89.       if (tmp == db->total_word_count)
  90.        goto done;
  91.       k=0;
  92.       while (k<stored) {
  93.         if (tmp > keyvalue[k])
  94.       break;
  95.     k++;
  96.       }
  97.       if (k == stored) {
  98.         if (stored < 50) {
  99.       keyvalue[stored]= tmp;
  100. tmpval=strlen(dictionary_block_word(j, dictionary))+1;
  101.       keyword[stored++] = s_malloc(tmpval);
  102.       strcpy(keyword[stored-1], dictionary_block_word(j, dictionary));
  103.         }
  104.       } else {
  105.     if (stored < 50)
  106.       stored++;
  107.     for (l=stored-1; l>k; l--) {
  108.       if (l==49)
  109.         free(keyword[l]);
  110.       keyword[l] = keyword[l-1];
  111.       keyvalue[l] = keyvalue[l-1];
  112.         }
  113. tmpval=strlen(dictionary_block_word(j, dictionary))+1;
  114.     keyword[k] = s_malloc(tmpval);
  115.     strcpy(keyword[k], dictionary_block_word(j, dictionary));
  116.     keyvalue[k] = tmp;
  117.       }
  118.     }
  119.   }
  120. done:
  121.   /* done getting, now cull and sort */
  122.   x = 0;
  123.   for (i=0; i<50 && x<0.2; i++)
  124.     x += ((double) keyvalue[i])/db->total_word_count;
  125.   limit = i;
  126.   /* patch from tovio@sage.ucs.uwa.edu.au to fix problem with empty
  127.    *    keywords array
  128.    */
  129.   if (i>0)
  130.   for (i=0; i<=limit-1; i++) {
  131.     for (j=i; j<limit; j++)
  132.       if (strcmp(keyword[i],keyword[j])>0) {
  133.     ptr=keyword[i]; keyword[i]=keyword[j]; keyword[j]=ptr;
  134.       }
  135.   }
  136. #ifdef WIN32
  137.   nKeys = (short)limit;
  138. #else
  139.   nKeys = limit;
  140. #endif
  141. }
  142.